from xml.etree import ElementTree

# XML = '''<annotation>
#     <folder>20191229</folder>
#     <filename>ZB000679_F0140E9A.bmp</filename>
#     <path>E:\houwm\20191229\ZB000679_F0140E9A.bmp</path>
#     <source>
#         <database>Unknown</database>
#     </source>
#     <size>
#         <width>0</width>
#         <height>0</height>
#         <depth>3</depth>
#     </size>
#     <segmented>0</segmented>
#     <object>
#         <name>img</name>
#         <pose>Unspecified</pose>
#         <truncated>0</truncated>
#         <difficult>0</difficult>
#         <bndbox>
#             <xmin>245</xmin>
#             <ymin>150</ymin>
#             <xmax>783</xmax>
#             <ymax>674</ymax>
#         </bndbox>
#     </object>
# </annotation>
# '''

#(xmin, ymin, xmax, ymax)
def generate_annotation_xml(folder, filename, path, bnd_array):
    e = ElementTree.parse('template.xml')
    tree = e.getroot()

    for obj in tree.findall('folder'):
        obj.text = folder
    
    for obj in tree.findall('filename'):
        obj.text = filename

    for obj in tree.findall('path'):
        obj.text = path

    for obj in tree.findall("object"):
    
        bound = obj.find('bndbox')
        xmin = bound.find('xmin')
        xmin.text = str(bnd_array[0])

        ymin = bound.find('ymin')
        ymin.text = str(bnd_array[1])

        xmax = bound.find('xmax')
        xmax.text = str(bnd_array[2])

        ymax = bound.find('ymax')
        ymax.text = str(bnd_array[3])
